package com.kingsoft.dc.khaos.module.spark.util

import java.util.regex.Pattern
import java.text.SimpleDateFormat

/**
  * Created by WANGYING15 on 2019/7/31.
  */
object TechCheckRuleUtils {
  def main(args: Array[String]): Unit = {

    //    val value = "111111aa111"; // 纯数字
    //    println(isLetterDigit(value));


    //    val str = "1111111111"; // 纯数字
    //    println(strOverLength(null, "0"));


    //    Ã©Å,�,Ã©Å12�3123
        println(isMessyCode("�Ã3123"))
        println(replaceMessyCode("�Ã3123","jjjjj"))
    //    println(("一二三").length)


//    var s = "'sda" + "\n" + "edae'"
//    println(s.length)
//    println(isEscape(s))
    //println(replaceEscape(s))


    //    println("转换前：" + s)
    //    s = s.replaceAll("\r|\n|\0", "")
    //    println("转换后：" + s)


    //    val value = null;
    //    println(isLetter(value));
    //    println(isDigit(value));


    //    val str = "111111111211.1212"; // 纯数字
    //    println(intOverLength(str, "0"));
    //println(decimalOverLength("1111111.123", "10,2"));



    //    val str1 = "2000-01-1"
    //    val str2 = "2000-1-32"
    //    val str3 = "2007/02/20"
    //    println(isValidDate(str1))
    //    println(isValidDate(str2))
    //    println(isValidDate(str3))


    //    val str1 = "12:2:2"
    //    val str2 = "66:02:02"
    //    val str3 = "12/02/02"
    //    println(isValidTime(null))
    //    println(isValidTime(str2))
    //    println(isValidTime(str3))


    //    val str1 = "2000-01-1 12:2:2"
    //    val str2 = "2000-01-1 66:02:02"
    //    val str3 = "2000/01/1 12:02:02"
    //    println(isValidDateTime(str1))
    //    println(isValidDateTime(str2))
    //    println(isValidDateTime(str3))

  }

  def isLetterDigit(str: String): Boolean = {
    if (str != null) {
      val regex = "^[a-z0-9A-Z]+$"
      return str.matches(regex)
    }
    false
  }

  def strOverLength(str: String, len: String): Boolean = {
    if (len != null && !len.trim.equals("")) {
      if (str != null && len.toInt >= 0) {
        if(str.length > len.toInt) {
          return true
        }
      }
    }
    false
  }

  def isMessyCode(str: String): Boolean = {
    if (str != null) {
      var i = 0
      while (i < str.length) {
        val c = str.charAt(i)
        // 当从Unicode编码向某个字符集转换时，如果在该字符集中没有对应的编码，则得到0x3f（即问号字符?）
        //从其他字符集向Unicode编码转换时，如果这个二进制数在该字符集中没有标识任何的字符，则得到的结果是0xfffd
        if (c.toInt == 0xfffd) { // 存在乱码
          return true
        }
        i += 1
      }
    }
    false
  }

  def replaceMessyCode(str: String, replacedValue: String): String = {
    var i = 0
    val strBuffer = new StringBuffer()
    while (i < str.length) {
      val c = str.charAt(i)
      if (c.toInt == 0xfffd) { // 存在乱码
        strBuffer.append(replacedValue)
      } else {
        strBuffer.append(c)
      }
      i += 1
    }
    strBuffer.toString
  }

//  def replaceMessyCode(str: String, replacedValue: String): String = {
//    var i = 0
//    val strBuffer = new StringBuffer(str)
//    while (i < str.length) {
//      val c = str.charAt(i)
//      if (c.toInt == 0xfffd) { // 存在乱码
//        strBuffer.setCharAt(i, '?')
//      }
//      i += 1
//    }
//    strBuffer.toString
//  }

  def isEscape(str: String): Boolean = {
    if (str != null) {
      val p = Pattern.compile("\r|\n|\0")
      val m = p.matcher(str)
      return m.find()
    }
    false
  }

  def replaceEscape(str: String, replacedValue: String): String = {
    var dest = ""
    val p = Pattern.compile("\r|\n|\0")
    val m = p.matcher(str)
    dest = m.replaceAll(replacedValue)
    dest
  }

  def isLetter(str: String): Boolean = {
    if (str != null) {
      val regex = "^[a-zA-Z]+$"
      return str.matches(regex)
    }
    false
  }

  def isDigit(str: String): Boolean = {
    if (str != null) {
      val regex = "^[0-9]+$"
      return str.matches(regex)
    }
    false
  }

  def intOverLength(str: String, len: String): Boolean = {
    if (len != null && !len.trim.equals("")) {
      if (str != null && len.toInt >= 0) {
        if(str.length > len.toInt) {
          return true
        }
      }
    }
    false
  }

  def decimalOverLength(str: String, len: String): Boolean = {
    if (len != null && !len.trim.equals("")) {
      //val strArr = str.split(".")
      val lenArr = len.split(",")
      if (str != null && lenArr(0).toInt >= 0 && lenArr(1).toInt >= 0) {
        if(str.length - 1 > lenArr(0).toInt || str.split("\\.")(1).length > lenArr(1).toInt) {
          return true
        }
      }
    }
    false
  }

  def isValidDate(str: String, format: String): Boolean = {
    val sd = new SimpleDateFormat(format)
    try {
      val formatter = sd.parse(str)
      return str.equals(sd.format(formatter))
    } catch {
      case e: Exception =>
        return false
    }
  }

  def isValidTime(str: String, format: String): Boolean = {
    val sd = new SimpleDateFormat(format)
    try {
      val formatter = sd.parse(str)
      return str.equals(sd.format(formatter))
    } catch {
      case e: Exception =>
        return false
    }
  }

  def isValidDateTime(str: String, format: String): Boolean = {
    val sd = new SimpleDateFormat(format)
    try {
      val formatter = sd.parse(str)
      return str.equals(sd.format(formatter))
    } catch {
      case e: Exception =>
        return false
    }
  }

  def isValidFormat(str: String, format: String): Boolean = {
    val sd = new SimpleDateFormat(format)
    try {
      val formatter = sd.parse(str)
      return str.equals(sd.format(formatter))
    } catch {
      case e: Exception =>
        return false
    }
  }

  def validFormatArray(str: String, formatArray: Array[String]): Boolean = {
    for (format <- formatArray) {
      if (isValidFormat(str, format)) {
        return true
      }
    }
    false
  }
}